package com.micro.monitoring;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class BasicConfirmButtonDialog extends Dialog {


    private final Context mContext;

    private final String yes;
    private final String point;




    private com.micro.monitoring.BasicConfirmButtonDialog.BasicConfirmButtonDialogListener mListener;
    private Button tvYes;
    private TextView tvPoint;


    public BasicConfirmButtonDialog(Context context, String yes, String point) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.yes = yes;
        this.point=point;
        initView();

    }
    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;

        getWindow().getDecorView().setPadding(0, 0, 0, 0);

        getWindow().setAttributes(layoutParams);

        setOnKeyListener(new OnKeyListener() {
            /**
             * Called when a key is dispatched to a dialog. This allows listeners to
             * get a chance to respond before the dialog.
             *
             * @param dialog  the dialog the key has been dispatched to
             * @param keyCode the code for the physical key that was pressed
             * @param event   the KeyEvent object containing full information about
             *                the event
             * @return {@code true} if the listener has consumed the event,
             * {@code false} otherwise
             */
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){

                }
                return false;
            }
        });
    }



    private void initView() {

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        @SuppressLint("InflateParams")
        View view = inflater.inflate(R.layout.dialog_confirm_button, null);

        tvYes = (Button) view.findViewById(R.id.tv_yes);
        tvPoint=(TextView)view.findViewById(R.id.tv_point);

        tvYes.setOnClickListener(new com.micro.monitoring.BasicConfirmButtonDialog.clickListener());



        setContentView(view);

        setData();
    }


    @SuppressLint("ResourceAsColor")
    private void setData() {
        tvYes.setText(yes);
        tvPoint.setText(point);
//        if (background.equals(Constant.GREEN)){
//            tvYes.setBackground(mContext.getResources().getDrawable(R.drawable.shape_rounded_corner_all_person));
//        }
    }

    public void setTvYesColor(int colorId) {//设置item的字体颜色
        if (tvYes != null) {
            tvYes.setTextColor(colorId);
        }
    }



    public void setTvPointColor(int colorId) {
        if (tvPoint != null) {
            tvPoint.setTextColor(colorId);
        }
    }

    public void setClickListener(com.micro.monitoring.BasicConfirmButtonDialog.BasicConfirmButtonDialogListener listener) {
        this.mListener = listener;
    }

    public interface BasicConfirmButtonDialogListener {

        void onTvYesListener();

    }


    private class clickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_yes:
                    if (mListener != null) {
                        mListener.onTvYesListener();
                    }
                    break;
            }
        }
    }

}
